home *** CD-ROM | disk | FTP | other *** search
/ Java Developer's Companion / Java Developer's Companion.iso / Javacup / PR8ADPL7.TAR / productivity_tools / PR8ADPL7 / JFSuser.java < prev    next >
Encoding:
Java Source  |  1996-05-23  |  2.1 KB  |  93 lines

  1. // JFSuser.java
  2. // A representation of one JFS user. Users are stored in the /etc/users file,
  3. // in the following format
  4. //
  5. //   USERS = { USER '\n' }*
  6. //
  7. //   USER = Name ':' Real ':'  Password ':' Homedir ':' groups { ':' Group }*
  8. //
  9. import java.io.*;
  10. import java.util.Vector;
  11. import LineInputStream;
  12. import JFSdirectory;
  13. import StringSplitter;
  14.  
  15. public class JFSuser
  16. {
  17.     String name;
  18.     String realname;
  19.     String password;
  20.     String home;
  21.     Vector groups = new Vector();
  22.  
  23.     // JFSuser
  24.     // Create a new user, read from a String
  25.     JFSuser(String s) throws IOException
  26.     {
  27.     StringSplitter tok = new StringSplitter(s,':');
  28.     if (tok.countTokens() < 5)
  29.         throw new IOException("user format error");
  30.     name = tok.nextToken();
  31.     realname = tok.nextToken();
  32.     password = tok.nextToken();
  33.     home = tok.nextToken();
  34.     int g = Integer.parseInt(tok.nextToken());
  35.     for(int i=0; i<g; i++)
  36.         groups.addElement(tok.nextToken());
  37.     }
  38.  
  39.     // JFSuser
  40.     // Create an empty JFSuser
  41.     JFSuser()
  42.     {
  43.     }
  44.  
  45.     // output
  46.     // Convert this user to a string
  47.     String output()
  48.     {
  49.     StringJoiner j = new StringJoiner(':');
  50.     j.add(name);
  51.     j.add(realname);
  52.     j.add(password);
  53.     j.add(home);
  54.     j.add(String.valueOf(groups.size()));
  55.     for(int i=0; i<groups.size(); i++) {
  56.         j.add((String)groups.elementAt(i));
  57.         }
  58.     return j.toString();
  59.     }
  60.  
  61.     // perms
  62.     // Returns the first set of permissions for this user (or one of this
  63.     // user's groups) for a given file.
  64.     String perms(JFSfile f)
  65.     {
  66.     if (name.equals("root"))
  67.         return "rwp";        // root can do anything
  68.     for(int i=0; i<f.users.size(); i++) {
  69.         JFSfileuser u = (JFSfileuser)f.users.elementAt(i);
  70.         if (u.name.equals(name))
  71.             return u.rwp;    // found this user
  72.         if (u.name.equals("all"))
  73.             return u.rwp;    // every user is implicitly in all
  74.         for(int j=0; j<groups.size(); j++)
  75.             if (((String)groups.elementAt(j)).equals(u.name))
  76.                 return u.rwp;    // found one of my groups
  77.         }
  78.     return "";
  79.     }
  80.  
  81.     // ingroup
  82.     // Is this user in the given group?
  83.     boolean ingroup(String g)
  84.     {
  85.     if (g.equals("all")) return true;
  86.     for(int i=0; i<groups.size(); i++)
  87.         if (((String)groups.elementAt(i)).equals(g))
  88.             return true;
  89.     return false;
  90.     }
  91. }
  92.  
  93.